package com.yigerende.controller;

import com.yigerende.pojo.OrderVo;
import com.yigerende.pojo.TOrder;
import com.yigerende.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    //增加订单
    @PostMapping("/save")
    public int save(@RequestBody TOrder order){
        return orderService.save(order);
    }

    //查询某个用户的订单(连接菜品表，用户表)(分页)(map传递3个，uid,start,end)
    @GetMapping("/findAllByUid/{uid}/{page}/{limit}")
    public OrderVo findAllByUid(@PathVariable("uid") Integer uid, @PathVariable("page") Integer page, @PathVariable("limit") Integer limit){
        Map map = new HashMap();
        map.put("uid",uid);
        map.put("start",(page-1)*limit);
        map.put("end",limit);
        OrderVo orderVo = new OrderVo();
        orderVo.setCode(0);
        orderVo.setMsg("");
        orderVo.setCount(orderService.countByUid(uid));
        orderVo.setData(orderService.findAllByUid(map));
        return orderVo;
    }

    //当菜品删除时，订单也删除
    @DeleteMapping("/deleteByMid/{mid}")
    public int deleteByMid(@PathVariable("mid") Integer mid){
        return orderService.deleteByMid(mid);
    }

    //用户删除订单
    @DeleteMapping("/deleteByUid/{uid}")
    public int deleteByUid(@PathVariable("uid") Integer uid){
        return orderService.deleteByUid(uid);
    }

    //通过用户是否付款查询数据(分页)(map传递state,start,end)(查询未付款的订单)
    @GetMapping("/findAllByState/{state}/{page}/{limit}")
    public OrderVo findAllByState(@PathVariable("state") Integer state, @PathVariable("page") Integer page, @PathVariable("limit") Integer limit){
        Map map = new HashMap();
        map.put("state",0);
        map.put("start",(page-1)*limit);
        map.put("end",limit);
        OrderVo orderVo = new OrderVo();
        orderVo.setCode(0);
        orderVo.setMsg("");
        orderVo.setCount(orderService.countByState(0));
        orderVo.setData(orderService.findAllByState(map));
        return orderVo;
    }

    //那个管理员审核的，审核修改aid对应管理员和state状态
    @PutMapping("/updateState/{id}/{state}/{aid}")
    public int updateState(@PathVariable("id") Integer id, @PathVariable("aid") Integer aid,@PathVariable("state") Integer state){
        return orderService.updateState(id,aid,state);
    }


}
